/*
 * License GNU LGPL
 * Copyright (C) 2012 Amrullah <amrullah@panemu.com>.
 */
package com.abc.cheque.common;

import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import javafx.scene.control.Control;
import javafx.scene.control.Tooltip;

/**
 *
 * @author Amrullah <amrullah@panemu.com>
 */
public class TiwulFXUtil {

    /**
     * TODO get default locale from configuration file
     */
    private static Locale loc = new Locale("en", "US");
    private static ResourceBundle literalBundle = ResourceBundle.getBundle("literal", loc);
    public static int DEFAULT_LOOKUP_SUGGESTION_ITEMS = 10;
    public static int DEFAULT_LOOKUP_SUGGESTION_WAIT_TIMES = 500;
    /**
     * Default number of rows displayed in {@link com.panemu.tiwulfx.table.TableControl TableControl}
     */
    public static int DEFAULT_TABLE_MAX_ROW = 500;
    

    public static Locale getLocale() {
        return loc;
    }

    public static void setLocale(Locale loc) {
        TiwulFXUtil.loc = loc;
        literalBundle = ResourceBundle.getBundle("literal", loc);
    }

    public static DecimalFormat getDecimalFormat() {
        NumberFormat nf = NumberFormat.getNumberInstance(loc);
        return (DecimalFormat) nf;
    }

    public static ResourceBundle getLiteralBundle() {
        return literalBundle;
    }

    public static void setLiteralBundle(ResourceBundle literalBundle) {
        TiwulFXUtil.literalBundle = literalBundle;
    }

    public static String getLiteral(String key) {
        try {
            return literalBundle.getString(key);
        } catch (Exception ex) {
            return key;
        }

    }
    
    public static String getLiteral(String key, Object... param) {
        try {
            return MessageFormat.format(literalBundle.getString(key), param);
        } catch (Exception ex) {
            return key;
        }

    }
    
    public static void setToolTip(Control control, String key) {
        control.setTooltip(new Tooltip(getLiteral(key)));
    }
    
    public static void openFile(String fileToOpen) throws Exception {
        boolean windows = false;
        boolean mac = false;
        String os = System.getProperty("os.name");
        if (os != null && os.startsWith("Windows")) {
            windows = true;
        } else if (os != null && os.toLowerCase().indexOf("mac") != -1) {
            mac = true;
        }
        String cmd;
        if (windows) {
            cmd = "rundll32 url.dll,FileProtocolHandler " + fileToOpen;
            Process p = Runtime.getRuntime().exec(cmd);
        } else if (mac) {
            cmd = "open " + fileToOpen;
            Process p = Runtime.getRuntime().exec(cmd);
        } else {
            cmd = "edit " + fileToOpen;
            Process p = Runtime.getRuntime().exec(cmd);
        }
    }
}
